import Vue from 'vue'
import App from './App.vue'
import './registerServiceWorker'
import router from './router'
import store from './store'


import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)

import uploader from 'vue-simple-uploader'
Vue.use(uploader)


axios.defaults.baseURL = 'http://localhost:8080/dev-api'

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  let token = localStorage.getItem("token");
  console.log(token);
  console.log('window.location.href',window.location.href);
  if (token == null && window.location.href!='http://localhost:8080/') {
    router.replace('/');
  }
  config.headers.token =  token;
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  console.log(response);
  if (response.data.code == 401) {
    ElementUI.Message({
      message: '请重新登录',
      type: 'success'
    })

    if (window.location.href != 'http://localhost:8080/') {
      router.replace("/");
    }
    return;
  }
  return response;
}, function (error) {
  console.log(error);
  if (error.response.data.code == 429) {
    ElementUI.Message({
      message: '请求太频繁了，稍后再试！！！',
      type: 'success'
    })
  }
  // 对响应错误做点什么
  console.log(error);
  return Promise.reject(error);
});

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
